/*
 *	misc.h
 *
 *	Created on: 2017年5月19日
 *	Author: NKJ
 */

#ifndef _MISC_H_
#define _MISC_H_

#include "stdio.h"
#include "stdbool.h"
#include "stdint.h"
#include "prg_version.h"

#define VERSION PRG_VERSION 

#define ATHEADER 	"AT+"
#define ATEND 		"\r\n"

enum
{
	STOP = 0,
	START= 1,
};

enum
{
	ADVER = 0,
	CLIENT= 1,
	SERVE = 2,
    ADVER_SERVE = 3,

    SHUTDOWN = 4,

    STYLELAST,
};

enum
{
	DISABLE = 0,
	ENABLE  = 1,
};

enum timer
{
	TIMER_ADV_STOP = 1,
    TIMER_ADV_START,
	TIMER_SCAN_START,
	TIMER_SCAN_STOP,
	TIMER_GATT_SEND,
	TIMER_CON_START,
	
	TIMER_LED,
};

enum _external_signal
{
	EXSG_WAKEUP = 1,
	EXSG_USART0_RX_DMA_DONE,
	EXSG_USART0_RX_DMA_TIMEOUT,
	EXSG_RECV_USR_ADV_DATA,
	EXSG_RECV_USR_AT_CMD,
};

enum _at_cmd_id
{
	ATCMD_NONE = 0x00,
	ATCMD_HEADER,
	ATCMD_END,
	ATCMD_PLUS,
	ATCMD_DFU,
	ATCMD_QUERY_MAC,
	ATCMD_QUERY_BLEWORKSTYLE,
	ATCMD_SET_BLEWORKSTYLE,
	ATCMD_QUERY_VERSION,
	ATCMD_SCANSTART,
	ATCMD_SCANSTOP,
	ATCMD_SET_DATAFILTER,
	ATCMD_GOTO_SLEEP,
	ATCMD_SET_NAME,
	ATCMD_QUREY_CONSTA,
	ATCMD_SET_HR,
    ATCMD_SET_BATLEVEL,
	ATCMD_SET_BATVOLTAGE,
    ATCMD_SET_PIN,
    ATCMD_DISCON,
    ATCMD_QUERY_ALL,
    ATCMD_SHUTDOWN,
    ATCMD_CONNECT,

	ATCMD_ERROR,
};

enum _msgid
{
    RETURN_MSG_ATCMD_NONE = 0x00,
	RETURN_MSG_ATCMD_HEADER,
	RETURN_MSG_ATCMD_END,
	RETURN_MSG_ATCMD_PLUS,
	RETURN_MSG_ATCMD_DFU,
	RETURN_MSG_ATCMD_QUERY_MAC,
	RETURN_MSG_ATCMD_QUERY_BLEWORKSTYLE,
	RETURN_MSG_ATCMD_SET_BLEWORKSTYLE,
	RETURN_MSG_ATCMD_QUERY_VERSION,
	RETURN_MSG_ATCMD_SCANSTART,
	RETURN_MSG_ATCMD_SCANSTOP,
	RETURN_MSG_ATCMD_SET_DATAFILTER,
	RETURN_MSG_ATCMD_GOTO_SLEEP,
	RETURN_MSG_ATCMD_SET_NAME,
	RETURN_MSG_ATCMD_QUREY_CONSTA,
	RETURN_MSG_ATCMD_SET_HR,
    RETURN_MSG_ATCMD_SET_BATLEVEL,
	RETURN_MSG_ATCMD_SET_BATVOLTAGE,
    RETURN_MSG_ATCMD_SET_PIN,
    RETURN_MSG_ATCMD_DISCON,
    RETURN_MSG_ATCMD_QUERY_ALL,
    RETURN_MSG_ATCMD_SHUTDOWN,
    RETURN_MSG_ATCMD_CONNECT,
	RETURN_MSG_ATCMD_ERROR,

    RETURN_MSG_SYS_BOOT = 0x80,
    RETURN_MSG_OPENED5,
    RETURN_MSG_OPENED4,
    RETURN_MSG_CLOSED,
    RETURN_MSG_NOTIFY_EN,
    RETURN_MSG_NOTIFY_DIS,
    RETURN_MSG_BONDED,
    RETURN_MSG_OVERFLOW,
    RETURN_MSG_DATAERROR,
    RETURN_MSG_WORKSTYLE,
    RETURN_MSG_MASTER_SERVE,
    RETURN_MSG_MASTER_NOTIFY,
    RETURN_MSG_MASTER_DATA,
};

typedef struct 
{
	enum _at_cmd_id id;
	char *atcmdstr;
	char *atcmdres;
}atcmd,*patcmd;

extern const atcmd atcmdass[];

typedef union
{
	uint8_t raw_data[31];
	struct
	{
		uint8_t header[8];
		uint8_t data_len;  		//数据长度
		uint8_t data_mode;  	//数据类型
        uint16_t company_id;    //厂商ID
		uint8_t data[19];  		//数据
	}frame;
}ubrddata,*pubrddata;

extern enum _at_cmd_id getATCMD(char *atcmd,uint8_t len);
extern uint8_t txpSetTransmitPower(int16_t txpTransmitPowerToSet);
extern int matoi(char* pstr);
extern uint32_t mint(char *data);
extern uint32_t mintl(char *data,uint16_t start,uint16_t end,uint8_t he);
extern uint8_t str2bdaddr(char str[],uint8_t bd[]);
extern void cpy2Array(uint8_t array1[],uint8_t array2[],uint8_t len);
extern uint8_t cmp2Array(uint8_t array1[],uint8_t array2[],uint8_t len);
extern uint8_t cmpi2Array(uint8_t array1[],uint8_t array2[],uint8_t len);

#endif /* INC_MISC_H_ */
